package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.TouSuBean;
import com.jucai.pppeiyouclient.comm.CommonMethod;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.view.popup.ComplaintReasonPopup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class ComplaintActivity extends BaseActivity {
    private static final String TAG = "ComplaintActivity : ";
    private CircleImageView complaintImg;
    private TextView complaintNick, complaintNo, complaintReason, complaintSubmit;
    private EditText complaintRemark;
    private ComplaintReasonPopup reasonPopup;
    private List<String> reasons = new ArrayList<>();

    private String order_sn, receiver_id;
    private TouSuBean touSuBean;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_complaint);
        ((TextView) findViewById(R.id.common_title_text)).setText("投诉");
        complaintImg = (CircleImageView) findViewById(R.id.complaint_img);
        complaintNick = (TextView) findViewById(R.id.complaint_nick);
        complaintNo = (TextView) findViewById(R.id.complaint_no);
        complaintReason = (TextView) findViewById(R.id.complaint_reason);
        complaintSubmit = (TextView) findViewById(R.id.complaint_submit);
        complaintRemark = (EditText) findViewById(R.id.complaint_remark);
    }

    @Override
    protected void initData() {
        order_sn = getIntent().getStringExtra("order_sn");
        receiver_id = getIntent().getStringExtra("receiver_id");

        reasonPopup = new ComplaintReasonPopup(ComplaintActivity.this, getWindow(), R.style.animBottomInBottomOut, null);
        getData();
    }

    private void getData() {
        Map<String, String> mapComplaint = new HashMap<>();
        mapComplaint.put("service", "Client_Compain.tousu");
        mapComplaint.put("torder_ordersn", order_sn);
        mapComplaint.put("tcompain_receiver_id", receiver_id);

        CommonRequestNetData.getJsonStringDataByEncrpt(ComplaintActivity.this, mapComplaint, new CommonRequestNetData.ResultCallBack() {
            @Override
            public void parseJsonString(String str) throws Exception {
                LogUtils.e(str);
                touSuBean = new Gson().fromJson(str, TouSuBean.class);
                setData();
            }

            @Override
            public void onError(String str) throws Exception {
                ToastUtils.showToast(str);
            }
        });
    }

    private void setData() {
        if (touSuBean != null) {
            complaintNo.setText(touSuBean.getPeiyouinfo().getTorder_ordersn());

            Glide.with(ComplaintActivity.this)
                    .load(touSuBean.getPeiyouinfo().getMember_avatar())
                    .override(60, 60)
                    .centerCrop()
                    .into(complaintImg);

            complaintNick.setText(touSuBean.getPeiyouinfo().getMember_realname());

            reasons.clear();
            for (int i = 0; i < touSuBean.getTcompain_reason().size(); i++) {
                String value = touSuBean.getTcompain_reason().get(i);
                reasons.add(value);

                if (i == 0) {
                    complaintReason.setText(value);
                }
            }

            reasonPopup.setPopupStrs(reasons, new CommonRecyclerViewAdapter.OnItemClickListener() {
                @Override
                public void onItemClickListener(View view, int position) {
                    complaintReason.setText(reasons.get(position));
                    reasonPopup.dismiss();
                }

                @Override
                public void onItemLongClickListener(View view, int position) {

                }
            });

        }
    }

    @Override
    protected void loadNetData() {

    }

    public static void startComplaint(Context context, String orderSn, String receiverId, String uId) {
        Intent intent = new Intent(context, ComplaintActivity.class);
        intent.putExtra("order_sn", orderSn);
        intent.putExtra("receiver_id", receiverId);
        intent.putExtra("u_id", uId);
        context.startActivity(intent);
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        complaintReason.setOnClickListener(this);
        complaintSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                CommonH5Activity.startCommH5(ComplaintActivity.this, 1,
                        SharedPreferencesUtil.getStringPreference(ComplaintActivity.this, "HISTORY", "his_url", ""),
                        null, getIntent().getStringExtra("order_id"), null);
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(ComplaintActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.complaint_reason:
                reasonPopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                CommonMethod.setWindowAlpha(0.5f, getWindow());
                break;

            case R.id.complaint_submit:

                if (TextCheckUtils.isEmpty(complaintReason.getText().toString())) {
                    ToastUtils.showToast("请填写备注");
                    return;
                }

                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Client_Compain.tousuinto");
                param.put("torder_ordersn", getIntent().getStringExtra("order_sn"));
                param.put("tcompain_receiver_id", getIntent().getStringExtra("receiver_id"));
                param.put("tcompain_reason", complaintReason.getText().toString());
                param.put("uid", getIntent().getStringExtra("u_id"));
                if (!TextUtils.isEmpty(complaintRemark.getText().toString())) {
                    param.put("tcompain_content", complaintRemark.getText().toString());
                }
                LogUtils.e(TAG + "complaint param = " + param.toString());

                CommonRequestNetData.getJsonStringData(ComplaintActivity.this, param, true, new CommonRequestNetData.JsonStringCallBack() {
                    @Override
                    public void parseJsonString(String str) {
                        LogUtils.e(TAG + "str = " + str);
                        ToastUtils.showToast("投诉成功");

                        CommonH5Activity.startCommH5(ComplaintActivity.this, 1,
                                SharedPreferencesUtil.getStringPreference(ComplaintActivity.this, "HISTORY", "his_url", ""),
                                null, getIntent().getStringExtra("order_id"), null);
                        ActivityCollector.removeActivity(ComplaintActivity.this);
                    }
                });
                break;
        }
    }
}
